from dataclasses import dataclass, field

import requests
from requests import Response as RequestResponse
from utils.data_util import DataUtil

@dataclass
class Request:
    method: str = None
    host: str = None
    path: str = None
    query: dict = None
    headers: dict = field(default_factory=dict)
    type: str = 'json'
    data: dict = None
    param: dict = None

    def send(self):
        env = DataUtil.load_yaml('env.yaml')
        self.host = env[env['default']]

        json_data = None
        raw = None
        if self.type == 'json':
            json_data = self.data
        elif self.type == 'xml':
            # 数据格式处理
            ...
        elif self.type == 'form':
            ...
        else:
            raise Exception("not exist format " + self.type)

        request_response = request.request(
            method = self.method,
            url = self.host + self.path,
            params = self.param,
            data = raw,
            headers = self.headers,
            cookies = None,
            files = None,
            timeout = None,
            proxies = None,
            verify = None,
            json = json_data,
        )
        r = Response(request_response)
        logger.debug(r.text)
        return r

@dataclass
class Response:

    def __init__(self, requests_response):
        self.r: RequestsResponse = requests_response

    def json(self):
        return self.r.json()

    @property
    def text(self):
        return self.r.text

    @property
    def status_code(self):
        return self.r.status_code